package com.modernyuanshiren;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Guess {

	private final List<String> answer;

	public Guess(String... nums) {
		answer = Arrays.asList(nums);
	}

	public Guess(String guessStr) {
		answer = Arrays.asList(guessStr.split("")).stream()
				.collect(Collectors.toList());
	}

	public int getLength() {
		return answer.size();
	}

	private Result check(Guess target) {
		int a = 0, b = 0;
		List<String> t = target.answer;
		for (String s : answer) {
			if (t.contains(s)) {
				if (t.indexOf(s) == answer.indexOf(s)) {
					a++;
				} else {
					b++;
				}
			}
		}
		return new Result(a, b);
	}

	public boolean match(Record record) {
		return record.getGuess().check(this).equals(record.getResult());
	}

	public String getAnswer() {
		return answer.stream().collect(Collectors.joining());
	}
}
